package com.cloudptt.wp.ui;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.wp.R;


public class UiDlg
{
	
	/**
	 * 寰楀埌鑷畾涔夌殑progressDialog
	 * @param context
	 * @param msg
	 * @return
	 */
	public static Dialog createLoadingDialog(Context context, String msg) {  
		  
        LayoutInflater inflater = LayoutInflater.from(context);  
        View v = inflater.inflate(R.layout.loading_dialog, null);// 寰楀埌鍔犺浇view  
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 鍔犺浇甯冨眬  
        // main.xml涓殑ImageView  
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);  
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 鎻愮ず鏂囧瓧  
        // 鍔犺浇鍔ㄧ敾  
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(  
                context, R.anim.loading_animation);  
        // 浣跨敤ImageView鏄剧ず鍔ㄧ敾  
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
        tipTextView.setText(msg);// 璁剧疆鍔犺浇淇℃伅  
  
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 鍒涘缓鑷畾涔夋牱寮廳ialog  
  
        loadingDialog.setCancelable(false);// 涓嶅彲浠ョ敤鈥滆繑鍥為敭鈥濆彇娑�  
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.FILL_PARENT,  
                LinearLayout.LayoutParams.FILL_PARENT));// 璁剧疆甯冨眬  
        loadingDialog.show();
        return loadingDialog;  
  
    }  
	
	private static AlertDialog alertdialog;
	private static DialogInterface.OnClickListener localListener;
	public static void showListDialog(Context context,String[] listName,DialogInterface.OnClickListener listener)
	{
//		new AlertDialog.Builder(context,R.style.alert_dialog)
//		.setTitle("")
//		.setItems(listName,lister)
//		.show();
		localListener = listener;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		View view = inflater.inflate(R.layout.alertdialog_simple_list,null);
		ListView lv  = (ListView)view.findViewById(R.id.test_alertdialog_listview);
		lv.setAdapter(new ArrayAdapter(context,R.layout.it_dialog_view,listName));
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				alertdialog.dismiss();
				localListener.onClick( alertdialog, position );
			}
			
		});
		alertdialog = new AlertDialog.Builder(context, R.style.dialog_exit)
			.create();
		alertdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);  
		
		Window window = alertdialog.getWindow();
        if (alertdialog != null && window != null) {
            WindowManager.LayoutParams attr = window.getAttributes();
            if (attr != null) {
                attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                attr.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                attr.gravity = Gravity.CENTER;//设置dialog 在布局中的位置
            }
            window.setAttributes(attr); 
        }

		alertdialog.show();
		alertdialog.setContentView( view );
//		alertdialog.getWindow( ).setLayout( 500, 300 );
	}
	
	private static Dialog warningDialog;
	
	public static Dialog createWarningDialog(Context context, String warningString, int drawableId){
		LayoutInflater inflater = LayoutInflater.from(context);  
        View v = inflater.inflate(R.layout.warning_dialog, null);
        ImageView warningImage = (ImageView) v.findViewById(R.id.warning_img);  
        TextView messageTV = (TextView) v.findViewById(R.id.warning_name);
        if(drawableId == 0){
        	warningImage.setImageResource(R.drawable.ptticon);
        }else{
        	warningImage.setImageResource(drawableId);
        }
        messageTV.setText(warningString);
        
        warningDialog = new Dialog(context, R.style.loading_dialog);
        
        Button confirmBtn = (Button)v.findViewById(R.id.warning_confirm_btn);
        
        confirmBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				warningDialog.dismiss();
			}
		});
        
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
        builder.setView(v);
        warningDialog = builder.create();
        return warningDialog; 
	}
}
